import {
  TODO_DEL_BY_ID,
  TODO_UPDATE_BY_ID,
  TODO_ADD,
  TODO_UPDATE_TYPE,
} from './actionTypes';

function loadNewTodoAPI() {
  const newTodo = {
    task: '异步返回的任务',
    id: Date.now(),
    isDone: false,
  };
  console.log('开始请求');
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('请求成功，2秒后,数据返回');
      resolve(newTodo);
    }, 2000);
  });
}

export const addAction = (task) => {
  return async (dispatch) => {
    const res = await loadNewTodoAPI();

    dispatch({ type: TODO_ADD, payload: res });
  };
};

export const delByIdAction = (id) => ({
  type: TODO_DEL_BY_ID,
  payload: id,
});

export const updateByIdAction = (payload) => ({
  type: TODO_UPDATE_BY_ID,
  payload,
});

export const updateTypeAction = (type) => ({
  type: TODO_UPDATE_TYPE,
  payload: type,
});
